﻿Public Class XSCSongAdd

    Public Overrides Function DoAddExist() As Boolean
        MyBase.DoAddExist()
        DoAddExist = True
        Dim rs As ADODB.Recordset = Nothing
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine("SELECT Song FROM XSCSong  WHERE Song=? And Songer=?")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput)
        If Me.txtSong.Text.Trim().ToUpper().Length > 0 Then
            pa.Size = Me.txtSong.Text.Trim().ToUpper().Length
        Else
            pa.Size = 10
        End If
        pa.Value = Me.txtSong.Text.Trim().ToUpper()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput)
        If Me.txtSonger.Text.Trim().ToUpper().Length > 0 Then
            pa.Size = Me.txtSonger.Text.Trim().ToUpper().Length
        Else
            pa.Size = 10
        End If
        pa.Value = Me.txtSonger.Text.Trim().ToUpper()
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        rs = ct.ExcCommandAsRs(cmd)
        If rs Is Nothing Then Exit Function
        If rs.EOF = False Then
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            rs = Nothing
            DoAddExist = False
            Me.txtSonger.Focus()
            ShowMessageBox("已经存在")
            Exit Function
        End If
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
    End Function

    Public Overrides Function DoAddCheck() As Boolean
        ' MyBase.DoCheck()
        DoAddCheck = False

        If Me.txtSonger.Text.Trim = String.Empty Then
            ShowMessageBox("作者不能为空")
            Me.txtSonger.Focus()
            Exit Function
        End If

        If Me.txtSong.Text.Trim = String.Empty Then
            ShowMessageBox("歌曲不能为空")
            Me.txtSonger.Focus()
            Exit Function
        End If

        DoAddCheck = True
    End Function

    Public Sub ClearForm()
        Me.txtSong.Text = ""
        Me.txtSonger.Text = ""
        Me.txtMemo.Text = "()"
        txtSonger.Focus()
    End Sub

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" XSCSong ")
        sb.AppendLine(" (")
        sb.AppendLine(" Song ")
        sb.AppendLine(" ,[index]")
        sb.AppendLine(" ,Songer")
        sb.AppendLine(" ,Memo")
        sb.AppendLine(" ,USERNAME ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,AddTime ")
        sb.AppendLine(" ,OptUser ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ) ")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 10
        pa.Value = CMainModule.CmnToStr1(Me.txtSong.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 15
        pa.Value = getPY(CMainModule.CmnToStr1(Me.txtSonger.Text).Trim())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 100
        pa.Value = CMainModule.CmnToStr1(Me.txtSonger.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 50
        pa.Value = txtMemo.Text.Trim
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            Me.bAdd = True '表示成功插入数据能刷新界面
            ShowMessageBox("插入成功")
            ClearForm()
            DoInsert = True
        Else
            DoInsert = False
        End If

    End Function

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ClearForm()
    End Sub

    Private Sub btnUpdateForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateForm.Click
        Using frm As ListSong1 = New ListSong1
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
        End Using
    End Sub

    Public Function getPYChar(ByVal SingleChar As String) As String
        getPYChar = ""
        Dim lChar As Long
        lChar = 65536 + Asc(SingleChar)
        If (lChar >= 45217 And lChar <= 45252) Then getPYChar = "A"
        If (lChar >= 45253 And lChar <= 45760) Then getPYChar = "B"
        If (lChar >= 47761 And lChar <= 46317) Then getPYChar = "C"
        If (lChar >= 46318 And lChar <= 46825) Then getPYChar = "D"
        If (lChar >= 46826 And lChar <= 47009) Then getPYChar = "E"
        If (lChar >= 47010 And lChar <= 47296) Then getPYChar = "F"
        If (lChar >= 47297 And lChar <= 47613) Then getPYChar = "G"
        If (lChar >= 47614 And lChar <= 48118) Then getPYChar = "H"
        If (lChar >= 48119 And lChar <= 49061) Then getPYChar = "J"
        If (lChar >= 49062 And lChar <= 49323) Then getPYChar = "K"
        If (lChar >= 49324 And lChar <= 49895) Then getPYChar = "L"
        If (lChar >= 49896 And lChar <= 50370) Then getPYChar = "M"
        If (lChar >= 50371 And lChar <= 50613) Then getPYChar = "N"
        If (lChar >= 50614 And lChar <= 50621) Then getPYChar = "O"
        If (lChar >= 50622 And lChar <= 50905) Then getPYChar = "P"
        If (lChar >= 50906 And lChar <= 51386) Then getPYChar = "Q"
        If (lChar >= 51387 And lChar <= 51445) Then getPYChar = "R"
        If (lChar >= 51446 And lChar <= 52217) Then getPYChar = "S"
        If (lChar >= 52218 And lChar <= 52697) Then getPYChar = "T"
        If (lChar >= 52698 And lChar <= 52979) Then getPYChar = "W"
        If (lChar >= 52980 And lChar <= 53640) Then getPYChar = "X"
        If (lChar >= 53689 And lChar <= 54480) Then getPYChar = "Y"
        If (lChar >= 54481 And lChar <= 52289) Then getPYChar = "Z"
        Return getPYChar
    End Function

    Public Function getPY(ByVal str As String) As String
        getPY = ""
        For i As Integer = 0 To Len(str) - 1
            getPY = getPY & getPYChar(Mid(str, i + 1, 1))
        Next
    End Function


    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()

        Me.Text = "歌曲修改"
        Me.txtSong.Enabled = False
        Me.txtSonger.Enabled = False
        Me.btnUpdateForm.Enabled = False
        Me.btnReset.Enabled = False

        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * FROM XSCSong  WHERE id='" + CStr(Me.参数列表("ID")) + "'")
        If rs Is Nothing Then Exit Sub
        If rs.EOF Then GoTo Function_End
        Me.txtSonger.Text = rs.Fields("Songer").Value.ToString()
        Me.txtSong.Text = rs.Fields("Song").Value.ToString()
        Me.txtMemo.Text = rs.Fields("Memo").Value.ToString()
Function_End:
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        rs = Nothing
    End Sub

    Public Overrides Function DoUpdateCheck() As Boolean
        Return True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        Return True
    End Function

    Public Overrides Function DoUpdate() As Boolean
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" UPDATE ")
        sb.AppendLine(" XSCSong ")
        sb.AppendLine(" SET   Memo = ? ")
        sb.AppendLine(" ,     OPTIME = ? ")
        sb.AppendLine(" ,     OptUser = ? ")
        sb.AppendLine(" WHERE Song = ? ")
        sb.AppendLine(" AND   Songer = ?")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 50
        pa.Value = txtMemo.Text.Trim
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 10
        pa.Value = CMainModule.CmnToStr1(Me.txtSong.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 30
        pa.Value = CMainModule.CmnToStr1(Me.txtSonger.Text).Trim()
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            Me.bAdd = True '表示成功插入数据能刷新界面
            ShowMessageBox("修改成功")
            Me.Close()
            DoUpdate = True
        Else
            ShowMessageBox("修改失败")
            Me.Close()
            DoUpdate = False
        End If
    End Function

    Private Sub btn歌词_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn歌词.Click
        Using frm As New LRCForm
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then

            End If
        End Using
    End Sub
End Class
